<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        size="medium"
        label-width="100px"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入权限名称"
            clearable
            :style="{ width: '100%' }"
          />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input
            v-model="form.code"
            placeholder="请输入权限标识"
            clearable
            :style="{ width: '100%' }"
          />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input
            v-model="form.description"
            placeholder="请输入权限描述"
            clearable
            :style="{ width: '100%' }"
          />
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch v-model="form.enVisible" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { addPermission, updatePermission } from '@/api/permisson'
export default {
  components: {},
  inheritAttrs: false,
  props: [],
  data() {
    return {
      form: {
        name: undefined,
        code: undefined,
        description: undefined,
        enVisible: false,
        pid: '0',
        type: 1
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入权限名称',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '请输入权限标识',
            trigger: 'blur'
          }
        ],
        description: []
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onOpen() {},
    onClose() {
      this.$refs['formRef'].resetFields()
    },
    close() {
      // this.$emit('close')
      // //手动清空
      // this.form = {
      //   name: undefined,
      //   code: undefined,
      //   description: undefined,
      //   enVisible: false
      // }
      // 通过$options清空
      // this.form = this.$options.data().form
      this.$emit('close')
    },
    handelConfirm() {
      this.$refs['formRef'].validate(async(valid) => {
        if (!valid) return
        if (this.form.enVisible) {
          this.form.enVisible = '1'
        } else {
          this.form.enVisible = '0'
        }
        if (this.$attrs.title === '编辑权限') {
          await updatePermission(this.form)
        } else {
          console.log(this.form)
          await addPermission(this.form)
        }

        this.$message.success('操作成功')
        this.$parent.getList()
        this.close()
      })
    }
  }
}
</script>
<style></style>
